<template>
	<view class="container">
		<view class="content">
			<view class="left">
				<view class="top-list">
					<view class="top-item" v-for="topData in list" :key="topData.id" :class="activeData.id==topData.id ? 'active' :''"
						@click="changeTop(topData)">
						{{topData.name}}
					</view>
					<view class="top-item" @click="toAdd"> 添加 </view>
				</view>
			</view>
			<view class="right">
				<view class="" @click="toAddChild" v-if="chirldrenList.length <= 0"  style="margin-top: 100rpx;">
					<u-empty mode="list"  text="添加小类" @click="toAddChild"> </u-empty>
				</view>
				<view class="child-list" v-else>
					<view class="child-item" v-for="childData in chirldrenList" :key="childData.id" @click="childClick(childData)">
						{{childData.name}}
					</view>
					<view class="child-item-add" @click="toAddChild"> 添加小类 </view>
				</view>
			</view>
		</view>
		<view class="bottom">
			<view class="bottom-btn btn-tc" v-if="editFlag" @click="editFlag = false">退出编辑</view>
			<view class="bottom-btn" v-else @click="editFlag = true">编辑</view>
		</view>
	</view>
</template>

<script>
	import {getPidSzclassify} from '@/api/ledger.js'
	export default {
		data() {
			return {
				list: [],
				activeData: {},
				chirldrenList: [],
				editFlag: false
			}
		},
		onShow() {
			this.editFlag = false
			this.getPidSzclassify()
		},
		methods: {
			getPidSzclassify(){
				getPidSzclassify().then(res=>{
					this.list = res.data.data
					if(this.list.length > 0) {
						this.activeData = this.list[0]
						this.getPidSzclassifyChildren()
					}
				})
			},
			getPidSzclassifyChildren(){
				getPidSzclassify({pid:this.activeData.id}).then(res=>{
					this.chirldrenList = res.data.data
				})
			},
			toAdd() {
				uni.navigateTo({
					url: '/pages-landlord/ledger/szclassifyAdd'
				})
			},
			toAddChild(){
				uni.navigateTo({
					url: '/pages-landlord/ledger/szclassifyAdd?pid='+this.activeData.id+'&name='+this.activeData.name
				})
			},
			changeTop(data){
				if(this.editFlag) {
					uni.navigateTo({
						url: '/pages-landlord/ledger/szclassifyEdit?id='+data.id
					})
				}else {
					this.activeData = data
					this.getPidSzclassifyChildren()
				}
			},
			childClick(data){
				if(this.editFlag) {
					uni.navigateTo({
						url: '/pages-landlord/ledger/szclassifyEdit?id='+data.id
					})
				}else {
					uni.navigateBack({
							delta: 1,
							success: () => {
									// 通过事件或全局方式传参
									uni.$emit('szclassifySelected', data);
							}
					});
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		background-color: #ffffff;
	}
	.content {
		position: relative;
		display: flex;
		height: calc(100vh - constant(safe-area-inset-bottom) - 80rpx)!important;
		height: calc(100vh - env(safe-area-inset-bottom) - 80rpx) !important;
	}
	.left {
		width: 300rpx;
		height: 100%;
		background-color: #f8f9fb;
		overflow-y: auto;
		.top-item {
			padding: 30rpx;
			font-size: 30rpx;
			font-weight: 700;
		}
		.top-item.active {
			background-color: #ffffff;
		}
	}
	.right{
		width: 400rpx;
		height: 100%;
		overflow-y: auto;
		padding: 0 20rpx 0 40rpx;
		.child-item {
			padding: 30rpx;
			font-size: 30rpx;
			border-bottom: 2rpx solid #f0eff4;
		}
		.child-item-add {
			padding: 30rpx;
			font-size: 30rpx;
			text-align: center;
		}
	}
	.bottom {
		width: 100vw;
		height: calc(constant(safe-area-inset-bottom) + 80rpx);
		height: calc(env(safe-area-inset-bottom) + 80rpx);
		border-top: 1rpx solid #f0eff4;
		background-color: #f8f9fb;
	}
	.bottom-btn {
		padding: 20rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx)!important;
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx) !important;
	}
	.btn-tc {
		width: 100%;
		text-align: center;
		background-color: #ffc107;
		color: #fff;
		font-weight: 700;
	}
</style>